package com.delaphone.fadmin.service;

import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;
import org.jpos.gl.GLException;
import org.jpos.gl.GLSession;
import com.delaphone.fadmin.model.FAdmins;
import com.delaphone.flocash.model.FUser;
import java.util.ArrayList;
import java.util.Date;
import org.hibernate.criterion.Projections;

/**
 *
 * @author dongtv
 */
public class CustomerServiceImp implements CustomerService {
    
    private Logger log = Logger.getLogger("CustomerService");
    
    public List<FUser> findCustomers(Date start, Date end, String email, String firstName, String lastName, String city, String country, int pageSize, int pageNumber) {
        GLSession gls = null;
        try {
            gls = new GLSession("system");
            Criteria crit = gls.session().createCriteria(FUser.class);
            if (email != null && email.isEmpty()) {
                email = null;
            }
            if (firstName != null && firstName.isEmpty()) {
                firstName = null;
            }
            if (lastName != null && lastName.isEmpty()) {
                lastName = null;
            }
            if (city != null && city.isEmpty()) {
                city = null;
            }
            if (country != null && country.isEmpty()) {
                country = null;
            }
            if (start != null && start.equals(end)) {
                crit.add(Restrictions.eq("created", start));
            } else {
                if (start != null) {
                    crit.add(Restrictions.ge("created", start));
                }
                if (end != null) {
                    crit.add(Restrictions.le("created", end));
                }
            }
            //email
            if (email != null) {
                crit.add(Restrictions.like("email", email));
            }
            //First Name
            if (firstName != null) {
                crit.add(Restrictions.like("firstName", firstName));
            }
            //LastName
            if (lastName != null) {
                crit.add(Restrictions.like("lastName", lastName));
            }
            //City
            if (city != null) {
                crit.add(Restrictions.eq("city", city));
            }
            //country
            if (country
                    != null) {
                crit.add(Restrictions.eq("countryCode", country));
            }
            if (pageSize > 0 && pageNumber > 0) {
                crit.setMaxResults(pageSize);
                crit.setFirstResult(pageSize * (pageNumber - 1));
            }
//            log.info("-------------------------------------");
//            log.info("email:" + email);
//            log.info("firstname:" + firstName);
//            log.info("lastname:" + lastName);
//            log.info("city:" + city);
            return crit.list();
        } catch (HibernateException hex) {
            log.error("HibernateException:", hex);
        } catch (GLException gex) {
            log.error("GLException:", gex);
        }
        return new ArrayList<FUser>(0);
        
    }
    
    public long findCustomersRowCount(Date start, Date end, String email, String firstName, String lastName, String city, String country) {
        GLSession gls = null;
        try {
            gls = new GLSession("system");
            Criteria crit = gls.session().createCriteria(FUser.class);
             if (email != null && email.isEmpty()) {
                email = null;
            }
            if (firstName != null && firstName.isEmpty()) {
                firstName = null;
            }
            if (lastName != null && lastName.isEmpty()) {
                lastName = null;
            }
            if (city != null && city.isEmpty()) {
                city = null;
            }
            if (country != null && country.isEmpty()) {
                country = null;
            }
            if (start != null && start.equals(end)) {
                crit.add(Restrictions.eq("created", start));
            } else {
                if (start != null) {
                    crit.add(Restrictions.ge("created", start));
                }
                if (end != null) {
                    crit.add(Restrictions.le("created", end));
                }
            }
            //email
            if (email != null) {
                crit.add(Restrictions.like("email", email));
            }
            //First Name
            if (firstName != null) {
                crit.add(Restrictions.like("firstName", firstName));
            }
            //LastName
            if (lastName != null) {
                crit.add(Restrictions.like("lastName", lastName));
            }
            //City
            if (city != null) {
                crit.add(Restrictions.eq("city", city));
            }
            //country
            if (country
                    != null) {
                crit.add(Restrictions.eq("countryCode", country));
            }
            crit.setProjection(Projections.rowCount());
            return (Long) crit.uniqueResult();
            
        } catch (HibernateException hex) {
            log.error("HibernateException:", hex);
        } catch (GLException gex) {
            log.error("GLException:", gex);
        }
        return 0;
    }
    
    public boolean updateCustomer(FUser newObj) {
        GLSession gls = null;
        Transaction tx = null;
        try {
            gls = new GLSession("system");
            tx = gls.beginTransaction();
            gls.session().update(newObj);
            tx.commit();
            return true;
        } catch (HibernateException hex) {
            log.error("HibernateException:", hex);
        } catch (GLException gex) {
            log.error("GLException:", gex);
        }
        return false;
    }
}
